package br.com.treinar.controller;

import java.util.Set;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import br.com.treinar.modelo.Contato;
import br.com.treinar.service.ListaContatoService;
import br.com.treinar.util.AppException;

@ManagedBean
@SessionScoped
public class ListaContatoMB {

	private Set<Contato> contatos;

	private ListaContatoService service;

	@PostConstruct
	private void init() {
		service = new ListaContatoService();
	}

	private void carregarContatos() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		FacesMessage facesMessage = null;
		try {
			contatos = service.listarContatos();
		} catch (AppException e) {
			facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), "");
			facesContext.addMessage(null, facesMessage);
		}
	}

	public Set<Contato> getContatos() {
		carregarContatos();
		return contatos;
	}

	public void setContatos(Set<Contato> contatos) {
		this.contatos = contatos;
	}

}
